//
//  NavigationBar.swift
//  tabSwitch
//
//  Created by Apple on 2021/6/11.
//

import SwiftUI

struct NavigationBar: View {
    @EnvironmentObject var modelData : TabItemsModelData
    var width: CGFloat = UIScreen.main.bounds.width
    var height: CGFloat = UIScreen.main.bounds.height
    var isScrolling: Bool
    var callback: TabSwitchCallBack?
    @State var circlePathRotateAngle = 0.0
    
    var body: some View {
        HStack{
            Button(action: {
                callback?.goBack()
                
            }){
                Image(systemName:"arrow.backward")
                    .resizable()
                    .frame(width: height/30, height: height/30)
                    .padding(EdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 10 ))
            }
            Spacer()
            
            Text(String(modelData.TabItemList.count))
                .font(.subheadline)
                .frame(width: height/30, height: height/33)
                .overlay(
                    RoundedRectangle(cornerRadius: 2, style: .continuous)
                        .stroke(Color.black, lineWidth: 2)
               )
            Text("窗口")
            .font(.title3)
            .minimumScaleFactor(0.2)
            .frame(width: width/8, height: height/25)
            
            Button(action:{
                
                //YRNKLog("tabSwitch", msg: modelData.toast.description)
                withAnimation(.easeInOut(duration: 0.5), {
                    modelData.toast = true 
                    modelData.displayMode = !modelData.displayMode
                    circlePathRotateAngle += 180
                })
            },label:{
                Image.init(systemName: "arrow.triangle.2.circlepath")
                    .rotationEffect(.degrees(circlePathRotateAngle))
            })
            
            //.scaleEffect(x: 0.5, y: 0.5, anchor: .center)
            Spacer()
            Button(action:{
                callback?.createNewTabView()
                print(modelData.TabItemList.count)
                //                let newItem : TabItem = TabItem(logo:Image("baidu"), title: "百度", url: "https://www.baidu.com",image: Image("bg"))
                //                modelData.TabItemList.append(newItem)

            }){
                Image(systemName: "plus")
                    .resizable(resizingMode: .stretch)
                    .frame(width: height/30, height: height/30)
                    .padding(EdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 10 ))
            }
        }
        .frame(width: width, height: height/15, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
        .background(Color.white)
        .mask(RoundedRectangle(cornerRadius: 0))
        .shadow(color: isScrolling ? .gray.opacity(0.5) : .white, radius: 2, x: 0, y: 4)
    }
}


struct NavigationBar_Previews: PreviewProvider {
    static var previews: some View {
        NavigationBar(isScrolling: true)
            .environmentObject(TabItemsModelData())
    }
}

